package cn.com.duiba.millionaire.center.spider.spdierjob;

import cn.com.duiba.millionaire.center.spider.common.PageConsumer;
import cn.com.duiba.millionaire.center.spider.dao.appinfo.DianpingAppDao;
import cn.com.duiba.millionaire.center.spider.entity.DianpingInfoEntity;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.selector.Html;

import java.util.List;

/**
 * Created by sunchangji on 2018/8/3.
 */
@Component
public class DianpingJob {

    private static final String TAGET_URL = "http://www.dianping.com/shop/";
    private static final String START_URL = "http://www.dianping.com/hangzhou/ch75/g2872o2p";

    @Autowired
    private DianpingAppDao dianpingAppDao;

    public PageConsumer pageConsumer = page -> {
        String url = page.getUrl().get();
        Html html = page.getHtml();
        if (url.startsWith(START_URL)) {
            List<String> targetUrls = html.xpath("//div[@id='shop-all-list']//li//div[@class='pic']").links().regex("http://www.dianping.com/shop/[0-9]+").all();
            if (CollectionUtils.isNotEmpty(targetUrls)) {
                page.addTargetRequests(targetUrls);
            }
        } else if (url.startsWith(TAGET_URL)) {//
            String shopName = html.xpath("//div[@class='basic-info']//div[@class='shop-name']//h1/text()").get();
            String address = html.xpath("//div[@class='basic-info']//div[@class='brief-info']//div[@class='address']/text()").get();
            String phone = html.xpath("//div[@class='phone']//span[@class='item J-phone-hide']/@data-phone").get();
            String cityName = html.xpath("//div[@class='breadcrumb']//div[@class='inner']//a[1]/text()").get();
            String cateName = html.xpath("//div[@class='breadcrumb']//div[@class='inner']//a[2]/text()").get();
            DianpingInfoEntity entity = new DianpingInfoEntity();
            entity.setAddress(address);
            entity.setShopName(shopName);
            entity.setPhone(phone);
            entity.setCateName(cateName);
            entity.setCity(cityName);
            entity.setSiteUrl(url);
            dianpingAppDao.insertEntity(entity);
        }
    };


}
